﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Cryptography;
using NTTHAO.CommonWeb;
using NTTHAO.Web.BL.Authentication;

public partial class Sites_Vigor_Webparts_User_Register : NTTHAO.CommonWeb.CommonUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string action = Request.QueryString["action"];
            string jsonResult = "{}";

            string name = Request.QueryString["name"];
            string username = Request.QueryString["username"];
            string pass = Request.QueryString["password"];
            string email = Request.QueryString["email"];

            name = name == null ? "" : name.Trim();
            username = username == null ? "" : username.Trim();
            pass = pass == null ? "" : pass.Trim();
            email = email == null ? "" : email.Trim();

            User user = this.Register(name, username, pass, email);

            if (user != null && user.ID > 0)
            {
                jsonResult = "{\"result\":\"OK\", \"message\":\"Đăng ký thành công.\"}";
            }
            else
            {
                jsonResult = "{\"result\":\"NG\", \"message\":\"Đăng ký không thành công.\"}";
            }

            Response.ClearContent();
            Response.HeaderEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "text/html";
            Response.Write(jsonResult);
        }
        catch (Exception ex) 
        {
            Response.ClearContent();
            Response.HeaderEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "text/html";
            Response.Write(ex.Message);
        }
    }

    public User Register(string name, string userName, string pass, string email)
    {
        try
        {
            User user = new User();
            user.FullName = name;
            user.UserName = userName.Trim();
            user.Password = pass;
            user.Email = email.Trim();

            user.Insert();
            return user;
        }
        catch { }

        return null;
    }
}
